<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--
@html_name: 09生成器
@description: The past cannot be redeemed, the future can be changed.
@author: Jason Black
@create: 2023/6/25 17:49:12
-->
<script>
    /**
     * 生成器：一个特殊的函数，可以实现异步编程
     *          可以使用yield来实现代码的分割
     *  调用方式：通过 "生成器名称.next()的方式调用"
     *  参数传递
     */
    function* f() {
        console.log("Jason Black")
    }

    f().next()

    function* m() {
        console.log("001")
        yield "Jason"
        console.log("002")
        yield "Black"
        console.log("003")
        yield "Jack"
        console.log("004")
        yield "Jackson"
        console.log("005")
    }

    let m_ = m()
    for (let i = 0; i < 5; i++) {
        console.log("分割代码块")
        m_.next()
    }

    console.log("生成器的迭代器")
    for (let item of m()) {
        console.log(item)
    }

    // region
    console.log("参数传递")

    function * m1(param) {
        console.log(param)
        let p1 = yield "Jason"
        console.log(p1)
        let p2 = yield "Black"
        console.log(p2)
        let p3 = yield "Jack"
        console.log(p3)
        let p4 = yield "Jackson"
        console.log(p4)
    }

    let m1_ = m1("param_")
    m1_.next()
    m1_.next('1')
    m1_.next('2')
    m1_.next('3')
    m1_.next('4')
    // endregion
</script>
</body>
</html>